<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Propertysubtype extends Model_Base {

    protected $_name = 'property_subtype';
    protected $_primaryKey = 'prstyp_id';

    
    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("prtyp" => "property_subtype"))
                ->where("prstyp_id = ?", $id)
                ->where("prstyp_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchEntryByREFId($id) {
        $select = $this->select()
                ->from(array("prtyp" => "property_subtype"))
                ->where("prstyp_ref_id = ?", $id)
                ->where("prstyp_status!= 2");
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchPropertySubType($where = "", $limit = "", $offset = "0", $order = 'prstyp_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('city' => 'property_subtype'))
                ->where("prstyp_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array('' => '-- Select Property subtype--');
            foreach ($data->toArray() as $_propsub_type) {
                $finalArray[$_propsub_type['prstyp_id']] = $_propsub_type['prstyp_title'];
            }
            return $finalArray;
        } else {
            return array('' => '-- Select Property subtype --');
        }
    }

    public function fetchAllPropertySubType($where = "", $limit = "", $offset = "0", $order = 'prstyp_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('city' => 'property_subtype'))
                ->where("prstyp_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array();
            foreach ($data->toArray() as $_propsub_type) {
                $finalArray[$_propsub_type['prstyp_id']] = $_propsub_type['prstyp_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get propertytype
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchLISTPropertySubType($where = "", $limit = "", $offset = "0", $order = "prstyp_id", $sort = "ASC" ,$group ="prstyp_ref_id") {
        $select = $this->select()
                ->from(array("prtyp" => "property_subtype"),array('*','lang_names' => new Zend_Db_Expr('GROUP_CONCAT(ln.lang_name)')))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "prtyp.prstyp_ln_id = ln.lang_id",array('lang_name'))
                ->join(array('type' => 'property_type'), "prtyp.prtyp_id = type.prtyp_id",array('prtyp_name'))
                ->where("prstyp_status != 2")
                ->where("prstyp_title != ''");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if($group != ""){
            $select->group($group);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getPropertysubtypeCount($where) {
        $select = $this->select()
                ->from(array("prtyp" => "property_subtype"), Array("cnt" => "count(*)"))
                 ->setIntegrityCheck(false)
                ->join(array('ln' => 'languages'), "prtyp.prstyp_ln_id = ln.lang_id",array('lang_name'))
                ->join(array('type' => 'property_type'), "prtyp.prtyp_id = type.prtyp_id",array('prtyp_name'))
                ->where("prtyp.prstyp_status != 2")
                ->where("prstyp_title != ''");
                #->group('prstyp_ref_id');
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deletePropertysubtype($prtypArray) {
        if (is_array($prtypArray)) {
            foreach ($prtypArray as $_propertytype) {
                $propertysubtypeData = Array();
                $propertysubtypeData = $this->fetchEntryById($_propertytype);
                if (count($propertysubtypeData) > 0) {
                    $data = array("prstyp_status" => "2");
                    $this->update($data, "prstyp_ref_id =" . (int) $_propertytype);
                }
            }
        } else {
            $prtyp_id = $prtypArray;
            $propertysubtypeData = $this->fetchEntryByREFId($prtyp_id);
            if (count($propertysubtypeData) > 0) {
                $data = array("prstyp_status" => "2");
                $this->update($data, "prstyp_ref_id =" . $prtyp_id);
            }
        }
    }


}
